//
//  PreventCheatController.m
//  NPS
//
//  Created by jadd on 15/12/10.
//  Copyright © 2015年 jadd. All rights reserved.
//

#import "PreventCheatController.h"

#define TEXT_Field CGRectMake(170, 10, self.view.bounds.size.width - 180, 40.0f)


@interface PreventCheatController ()

@end

@implementation PreventCheatController

@synthesize array,isModify,isSwitch,isModifySwitch;
@synthesize tempValues,textFieldBeingEdited;

-(void)switchAction:(id)sender
{
    isModifySwitch = YES;
    UISwitch *sw = (UISwitch *)sender;
    
    self.isSwitch = sw.on;
  
}

-(IBAction)textFieldDone:(id)sender{
    
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.textFieldBeingEdited = textField;
    
    if (textFieldBeingEdited != nil)
    {
        NSNumber *tagAsNum= [[NSNumber alloc] initWithInteger:textFieldBeingEdited.tag];
        [tempValues setObject:textFieldBeingEdited.text forKey: tagAsNum];
        
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    isModify = YES;
    NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([str length] == 0){
        
        return YES;
    }
    NSString *regex = @"^([12][0-9]|30|[1-9])$";
    NSPredicate *prd = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [prd evaluateWithObject:str];
    if(result == NO)
    {
        [Config showMessage:@"只能输入1~30之间的数字!"];
    }
    
    return result;
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    //[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:UserDefault_answerTimes];
}

//返回按钮事件
-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
    
    if(isModifySwitch == YES)
    {
        BOOL b = [[NSUserDefaults standardUserDefaults] boolForKey:UserDefault_answerOnce];
        if(b != self.isSwitch)
        {
            [[NSUserDefaults standardUserDefaults] setBool:self.isSwitch forKey:UserDefault_answerOnce];
          
        }
    }
    
    if(isModify == YES)
    {
        NSString *times = [[NSUserDefaults standardUserDefaults] objectForKey:UserDefault_answerTimes];
 
        if(textFieldBeingEdited != nil){
            if([times intValue] != [textFieldBeingEdited.text intValue])
            {
                [[NSUserDefaults standardUserDefaults] setObject:textFieldBeingEdited.text forKey:UserDefault_answerTimes];
               
            }
        }
    }
    
    if(isModifySwitch == YES || isModify == YES)
    {
        NSLog(@"post");
    }
    
}

- (void)viewDidLoad {
    self.title = @"防作弊设置";
    self.tableView.scrollEnabled = NO;
    isModify = NO;
    isModifySwitch = NO;
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom] ;
    button.frame = CGRectMake(0, 0, K_Global_BackButton_Width, K_Global_BackButton_Height);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateNormal];
    //[button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    self.array = [[NSMutableArray alloc] initWithObjects:@"每天最多回答1次",@"每月最多回答次数",nil];
    self.tempValues = [[NSMutableDictionary alloc] init];
    
    [super viewDidLoad];
    
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [array count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    
    static NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        if(indexPath.section == 1)
        {
            CGRect cect = TEXT_Field;
            
            UITextField *txtField = [[UITextField alloc] initWithFrame:cect];
            txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
            txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            txtField.font = [UIFont systemFontOfSize:18];
           
            txtField.textColor = GRAY;
            [txtField setDelegate:self];
            
            [txtField addTarget:self action:@selector(textFieldDone:) forControlEvents: UIControlEventEditingDidEndOnExit];
            [cell.contentView addSubview:txtField];
        }

    }
    
    NSUInteger section = [indexPath section];
    cell.textLabel.text = [self.array objectAtIndex:section];
    
    if(section == 0)
    {
        UISwitch *sthAuto = [[UISwitch alloc] init];
        [sthAuto addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        sthAuto.on = [[NSUserDefaults standardUserDefaults] boolForKey:UserDefault_answerOnce];
        cell.accessoryView = sthAuto;
    }
    
    
    UITextField *textField = nil;
    for (UIView *oneView in cell.contentView.subviews)
    {
        if ([oneView isMemberOfClass:[UITextField class]])
            textField = (UITextField *)oneView;

    }
    
    //label.text = [array objectAtIndex:row];
    NSNumber *rowAsNum = [[NSNumber alloc] initWithInteger:section];
    switch (section) {
        case 1:
            textField.placeholder = @"请输入1~30之间的数字";
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.textAlignment = NSTextAlignmentCenter;
            [textField setValue:[UIFont boldSystemFontOfSize:17] forKeyPath:@"_placeholderLabel.font"];
            //[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
            if ([[tempValues allKeys] containsObject:rowAsNum])
                textField.text = [tempValues objectForKey:rowAsNum];
            else
                textField.text = [[NSUserDefaults standardUserDefaults] objectForKey:UserDefault_answerTimes];
            break;
                default:
            break;
    }
    if (textFieldBeingEdited == textField)
        textFieldBeingEdited = nil;
    
    textField.tag = section;

    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



@end
